/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld;

import com.sun.spot.core.resources.transducers.ITriColorLED;
import com.sun.spot.core.resources.transducers.LEDColor;
import com.sun.spot.core.util.Utils;
import com.sun.spot.edemo.EDemoBoard;

/**
 *
 * @author me
 */
public class Buffer {
    private static ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
    private static int head=0, tail=0;
    private static boolean showLEDs = true;
    
    public void enqueue(LEDColor color) {
        while (showLEDs && head == (tail-1)%leds.length) {
            Utils.sleep(100);
        }
        leds[head].setColor(color);
        leds[head].setOn(showLEDs);
        Utils.sleep(100); //simulated processing time to exacerbate thread-synch problems
        head = (head+1)%leds.length;
    }
    
    public void dequeue() {
        while (showLEDs && head == tail) {
            Utils.sleep(100);
        }
        leds[tail].setOff();
        Utils.sleep(100); //simulated processing time to exacerbate thread-synch problems
        tail = (tail+1)%leds.length;
    }
    
    public void shutdown() {
        for (int i=0; i<8; i++) {
            leds[i].setOff();
        }
        showLEDs = false;
    }
    
    public void restart() {
        head = 0;
        tail = 0;
        showLEDs = true;
    }
}
